﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace ARMIR
{
    public static class ARMIR_Draw {
        /// <summary>
        /// 绘制空心方框
        /// </summary>
        /// <param name="centerWorldPos"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void DrawWhiteHollowBlock(Vector3 centerWorldPos, float width, float height, bool crossLine, float lineWidth)
        {
            Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(1f, 1f, 1f, 1f));
            float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();

            // 计算四个角的位置
            float halfWidth = width / 2f;
            float halfHeight = height / 2f;

            Vector3 bottomLeft = new Vector3(centerWorldPos.x - halfWidth, altitude, centerWorldPos.z - halfHeight);
            Vector3 bottomRight = new Vector3(centerWorldPos.x + halfWidth, altitude, centerWorldPos.z - halfHeight);
            Vector3 topRight = new Vector3(centerWorldPos.x + halfWidth, altitude, centerWorldPos.z + halfHeight);
            Vector3 topLeft = new Vector3(centerWorldPos.x - halfWidth, altitude, centerWorldPos.z + halfHeight);

            // 绘制方框
            GenDraw.DrawLineBetween(bottomLeft, bottomRight, whiteMat,lineWidth);
            GenDraw.DrawLineBetween(bottomRight, topRight, whiteMat,lineWidth);
            GenDraw.DrawLineBetween(topRight, topLeft, whiteMat,lineWidth);
            GenDraw.DrawLineBetween(topLeft, bottomLeft, whiteMat,lineWidth);
            //GenMapUI.DrawText(new Vector2(topRight.x,topRight.z + 0.1f), "嘻嘻我一定要活下去", new Color(1,1,1,1));
            //GenDraw.DrawCircleOutline(centerWorldPos,width,whiteMat);

            //绘制交叉线
            if (crossLine) {
                GenDraw.DrawLineBetween(topLeft, bottomRight, whiteMat, lineWidth);
                GenDraw.DrawLineBetween(topRight, bottomLeft, whiteMat, lineWidth);
            }
        }
        /// <summary>
        /// 绘制正十字线(好像没有用
        /// </summary>
        /// <param name="centerWorldPos">awa</param>
        /// <param name="lineLenth"></param>
        /// <param name="rotation"></param>
        public static void DrawCrossLine(Vector3 centerWorldPos,float lineLenth)
        {
            Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(1f, 1f, 1f, 1f));
            float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();
            float lineWidth = 0.05f;

            //计算四角位置
            Vector3 LineAStart = new Vector3(centerWorldPos.x + lineLenth, altitude, centerWorldPos.z);
            Vector3 LineAEnd = new Vector3(centerWorldPos.x - lineLenth, altitude, centerWorldPos.z);
            Vector3 LineBStart = new Vector3(centerWorldPos.x, altitude, centerWorldPos.z + lineLenth);
            Vector3 LineBEnd = new Vector3(centerWorldPos.x, altitude, centerWorldPos.z - lineLenth);

            //绘制正十字
            GenDraw.DrawLineBetween(LineAStart, LineAEnd, whiteMat, lineWidth);
            GenDraw.DrawLineBetween(LineBStart, LineBEnd, whiteMat, lineWidth);
        }
        /// <summary>
        /// 绘制根据环而显现方块列阵
        /// </summary>
        /// <param name="centerWorldPos"></param>
        /// <param name="cirLen"></param>
        /// <param name="baseHyaline">基础透明度0~1</param>
        /// <param name="ringWidth">环宽度</param>
        /// <param name="gapEachBlock">每个方块之间的间隔</param>
        /// <param name="blockSize">整个列阵的尺寸</param>
        public static void DrawBlocksWithCir(Vector3 centerWorldPos,float cirLen,float baseHyaline,float blockSize,float ringWidth,float gapEachBlock)
        {
            float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();
            float blockLenth = blockSize;
            //遍历所有阵列方格,根据距离环的距离设定透明度渲染列阵
            Vector3 indexVec3 = new Vector3(centerWorldPos.x - blockLenth, altitude, centerWorldPos.z + blockLenth); //从左上角开始向右逐渐下行遍历
            while (indexVec3.z > centerWorldPos.z - blockLenth) {
                float DisToCenter = Vector3.Distance(indexVec3, centerWorldPos);
                float DisToRing = DisToCenter - cirLen; //计算出方格距离圆环的距离
                DisToRing = DisToRing / ringWidth;
                float Hyaline = 0;
                if (DisToRing < 0) {
                    Hyaline = (1 - Math.Abs(DisToRing)) * baseHyaline;
                }else {
                    Hyaline = 0;
                }
                if (Hyaline > 1 || Hyaline < 0.1) {
                    Hyaline = 0;
                }
                if (Hyaline != 0 && DisToCenter < blockSize) {
                    Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.302f, 0.941f, 0.914f, Hyaline));
                    GenDraw.DrawLineBetween(indexVec3, new Vector3(indexVec3.x, indexVec3.y, indexVec3.z - 0.3f), whiteMat, 0.3f); //绘制小方框
                }
                //迭代器
                indexVec3 = new Vector3(indexVec3.x + gapEachBlock, indexVec3.y, indexVec3.z);
                if (indexVec3.x > centerWorldPos.x + blockLenth) {
                    indexVec3 = new Vector3(centerWorldPos.x - blockLenth, indexVec3.y, indexVec3.z - gapEachBlock);
                }
            }
        }
    }
    
}
